package org.extremengine.shape;

import org.extremengine.common.Point;

/**
 * 圆形，包含圆心坐标和圆半径。
 * @author yuxing
 *
 */
public class Circle extends Shape {

	private Point heart;
	private double radius;

	public Circle(Point heart, double radius) {
		super();
		this.heart = heart;
		this.radius = radius;
	}

	public Circle(double x, double y, double radius) {
		super();
		this.heart = new Point(x, y);
		this.radius = radius;
	}
	
	/**
	 * Whether point in the circle.
	 * @param p
	 * @return
	 */
	public boolean containsPoint(Point p) {
		return containsPoint(p.x, p.y);
	}
	
	/**
	 * Whether point (x.y) in the circle.
	 * @param x
	 * @param y
	 * @return
	 */
	public boolean containsPoint(double x, double y) {
		double cx = this.getHeart().getX();
		double cy = this.getHeart().getY();
		if (Math.sqrt((cx - x) * (cx - x) + (cy - y) * (cy - y)) <= this.getRadius()) {
			return true;
		}
		return false;
	}
	
	public static Circle wrap(Shape shape) {
		if (shape instanceof Circle) {
			return (Circle) shape;
		}
		else if (shape instanceof ParallelRectangle) {
			ParallelRectangle rect = (ParallelRectangle) shape;
			double radius = rect.getDiagonalLength() / 2;
			return new Circle(rect.getBarycenter(), radius);
		}
		else if (shape instanceof Rectangle) {
			Rectangle rect = (Rectangle) shape;
			double radius = rect.getDiagonalLength() / 2;
			return new Circle(rect.getBarycenter(), radius);
		}
		else if (shape instanceof Polygon) {
			throw new UnsupportedOperationException();
		}
		return null;
	}

	@Override
	public Point getBarycenter() {
		return heart;
	}

	@Override
	public void transform(double deltax, double deltay) {
		this.heart.setX(this.heart.x + deltax);
		this.heart.setY(this.heart.y + deltay);
	}

	@Override
	public Shape clone() {
		return new Circle(new Point(this.heart.x, this.heart.y), this.getRadius());
	}

	public Point getHeart() {
		return heart;
	}

	public void setHeart(Point heart) {
		this.heart = heart;
	}

	public double getRadius() {
		return radius;
	}

	public void setRadius(double radius) {
		this.radius = radius;
	}

	@Override
	public String toString() {
		return "[HEART:" + heart + ", RADIUS:" + radius + "]";
	}

}
